package com.clss.emergencycall.utils;

import android.app.Activity;

import java.util.LinkedList;
import java.util.List;

/**
 * 维护activity静态链表
 * Created by liuv on 2017/6/30.
 */

public class ActivityTracker {
    private static final String TAG = "ActivityTracker";
    private final static List<Activity> sActivities = new LinkedList<Activity>();

    public final static void onCreate(Activity activity) {
        if (activity == null) {
            throw new IllegalArgumentException("Activity instance is null");
        }
        sActivities.add(activity);
    }

    public final static void onDestory(Activity activity) {
        if (activity == null) {
            throw new IllegalArgumentException("Activity instance is null");
        }
        sActivities.remove(activity);
    }

    public final static void finishAll() {
        for (Activity activity : sActivities) {
            if (activity.getParent() != null) {
                activity.getParent().finish();
            }
            activity.finish();
        }
        sActivities.clear();
    }

    public  static boolean isExitActivity(Class<?> clss){
        for (Activity activity :sActivities) {
            Lg.i(TAG, "isExitActivity:  栈中的"+ activity.getComponentName().getClassName());
            Lg.i(TAG, "isExitActivity:  传入的 "+ clss.getSimpleName());
            if (activity.getComponentName().getClassName().contains(clss.getSimpleName())){
                return true;
            }
        }
        return false;
    }


    public static String getFirstActivityName(){
        if (sActivities.size() == 0){
            return "";
        }
        return sActivities.get(sActivities.size()-1).getClass().getSimpleName();
    }

    public static List<Activity> getsActivities() {
        return sActivities;
    }

    public static int getActivityNum(){
        return sActivities.size();
    }
}
